package oop.core;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import oop.core.control.Controller;
import oop.core.entity.Avatar;
import oop.core.entity.Entity;
import oop.core.map.GameMap;
import oop.core.map.Location;
import oop.core.med.AreaEffectMediator;
import oop.core.med.AvatarMovementMediator;
import oop.core.med.DeathMediator;
import oop.core.med.InteractionItemMediator;
import oop.core.med.InteractionMediator;
import oop.core.med.InventoryManagementMediator;
import oop.core.med.ItemPickupMediator;
import oop.core.med.ItemPlacementMediator;
import oop.core.med.LevelUpMediator;
import oop.core.med.MovementLogic;
import oop.core.med.PauseMediator;
import oop.core.med.SkillCollisionDetection;
import oop.core.med.SkillCollisionHandler;
import oop.core.med.SkillMediatorFromHotkey;
import oop.core.med.TransactionMediator;
import oop.core.med.AreaEffectMediator;
import oop.core.skill.SkillHandler;
import oop.core.skill.SkillObject;
import oop.core.view.GamePanel;
import oop.core.view.SizeablePanel;
import oop.core.view.listeners.ActiveSkillMouseListener;
import oop.core.view.listeners.EquipmentMouseListener;
import oop.core.view.listeners.InventoryMouseListener;

public class Game {
	
	private Avatar avatar;
	private GameMap gameMap;
	private List<SizeablePanel> panels;
	private Controller controller;
	
	private InventoryMouseListener inventoryMouseListener;
	private EquipmentMouseListener equipmentMouseListener;
	private ActiveSkillMouseListener activeSkillMouseListener;
	
	private Scheduler scheduler;
	
	public Game(Avatar avatar, GameMap gameMap, List<SizeablePanel> panels, Controller controller, InventoryMouseListener inventoryMouseListener,
					EquipmentMouseListener equipmentMouseListener, ActiveSkillMouseListener activeSkillMouseListener)
	{
		this.avatar = avatar;
		this.gameMap = gameMap;
		this.panels = panels;
		this.controller = controller;
		
		this.inventoryMouseListener = inventoryMouseListener;
		this.equipmentMouseListener = equipmentMouseListener;
		this.activeSkillMouseListener = activeSkillMouseListener;
		
		scheduler = new Scheduler();
		//System.out.println(avatar.getOcc().getActiveSkill().get(0).getName());
		
		startNewGame();
	}
	
	public void startNewGame()
	{
		// TODO: Make mediators here, pray it works!!!
		// TODO: Clean it up
		ItemPickupMediator itemPickupMediator = new ItemPickupMediator(avatar, gameMap.getTakeableItemMap());
		InteractionItemMediator interactionItemMediator = new InteractionItemMediator(avatar, gameMap.getInteractiveItemVisitableMap());
		
		TransactionMediator transactionMediator = new TransactionMediator(avatar);
		InteractionMediator interactionMediator = new InteractionMediator(avatar, transactionMediator);
		
		MovementLogic movementLogic = new MovementLogic(gameMap);
		
		AvatarMovementMediator avatarMovementMediator = new AvatarMovementMediator(avatar, itemPickupMediator, interactionItemMediator, interactionMediator, gameMap, movementLogic, scheduler);
		controller.setMovementMediator(avatarMovementMediator);
		
		ItemPlacementMediator itemPlacementMediator = new ItemPlacementMediator(gameMap.getTakeableItemMap(), avatar);
		InventoryManagementMediator inventoryManagementMediator = new InventoryManagementMediator(avatar, itemPlacementMediator);
		
		inventoryMouseListener.setInventoryManagementMediator(inventoryManagementMediator);
		equipmentMouseListener.setAvatar(avatar);
		
		AreaEffectMediator areaEffectMediator = new AreaEffectMediator(gameMap.getAeMap(), gameMap.getEntityList(), scheduler, avatar);
		
		SkillHandler skillHandler = new SkillHandler(scheduler, gameMap);
		SkillMediatorFromHotkey skillMediatorFromHotkey = new SkillMediatorFromHotkey(skillHandler, avatar);
		activeSkillMouseListener.setSkillMediatorFromHotkey(skillMediatorFromHotkey);
		
		PauseMediator pauseMediator = new PauseMediator(scheduler, avatarMovementMediator, inventoryManagementMediator, skillMediatorFromHotkey);
		DeathMediator deathMediator = new DeathMediator(gameMap.getEntityList(), avatar, scheduler, pauseMediator);
		SkillCollisionHandler skillCollisionHandler = new SkillCollisionHandler( gameMap.getEntityList(), gameMap.getSkillObjectMap(), avatar, scheduler );
		SkillCollisionDetection skillDetectionMediator = new SkillCollisionDetection( avatar, gameMap.getEntityList(), gameMap.getSkillObjectMap(), skillCollisionHandler, scheduler );
		
		LevelUpMediator levelUpMediator = new LevelUpMediator(panels.get(0), scheduler, avatar.getDerStats(), avatar.getOcc().getInteractiveSkill(), avatar.getOcc().getPassiveSkill(), avatar.getOcc().getActiveSkill() );
		
		
		controller.setPauseMediator(pauseMediator);
		
		RunGame.println("Starting a new game as a " + avatar.getName());
		RunGame.printMatrix();
		RunGame.printMatrix();
		RunGame.printMatrix();
		
		// Start the game!
		GameLoop gameLoop = new GameLoop(scheduler, panels);
		Thread gameThread = new Thread(gameLoop);
		
		RunGame.changePanel(GamePanel.ID);
		
		gameThread.start();
	}
}
